<?php

namespace app\index\controller;

use app\index\model\KuaiDiOptionsModel;
use app\index\model\KuaiDiUserModel;

class Index
{

    public function getOptions()
    {
        $model = new KuaiDiOptionsModel();

        $res = $model->getOptionsList();
        return json($res);
    }

    public function register()
    {
        $openId = input('openId', "");
        if (empty($openId)) {
            $res['success'] = false;
            $res['desc'] = 'msgId 参数错误';
            $res['errorcode'] = '200022';
            return json($res);
        }
        $userName = input('userName', '');
        $avatar = input('avatar', '');
        $tel = input('tel', '');


        $model = new KuaiDiUserModel();
        $data = [
            'open_id' => $openId,
        ];
        if ($userName) {
            $data['user_name'] = $userName;
        }
        if ($avatar) {
            $avatar = str_replace(get_http_type() . $_SERVER['HTTP_HOST'], '', $avatar);
            $data['avatar'] = $avatar;
        }
        if ($tel) {
            $data['tel'] = $tel;
        }
        $res = $model->registerUser($data);
        return json($res);
    }

    public function upUserInfo()
    {
        $userName = input('userName', '');
        $avatar = input('avatar', '');
        $tel = input('tel', '');
        $userId = input('userId', 0);
        if (empty($userId)) {
            $res['success'] = false;
            $res['desc'] = 'msgId 参数错误';
            $res['errorcode'] = '200022';
            return json($res);
        }
        $model = new KuaiDiUserModel();

        $where['id'] = ['=', $userId];
        if ($avatar) {
            $avatar = str_replace(get_http_type() . $_SERVER['HTTP_HOST'], '', $avatar);
            $upData['avatar'] = $avatar;
        }
        if ($userName) {
            $upData['user_name'] = $userName;
        }
        if ($tel) {
            $upData['tel'] = $tel;
        }
        if ($upData) {
            $res = $model->upUserInfo($upData, $where);
            return json($res);
        }
        $res['success'] = true;
        $res['desc'] = '';
        $res['errorcode'] = '';
        return json($res);
    }

    public function getOpenId()
    {
        $jsCode = input('jsCode', "");
        if (empty($jsCode)) {
            $res['success'] = false;
            $res['desc'] = 'msgId 参数错误';
            $res['errorcode'] = '200022';
            return json($res);
        }

        $params = [
            'appid' => 'wx89c883f4472fe2b6',
            'secret' => '45c69923a90fcb60de1bbfb3f493aaee',
            'grant_type' => 'authorization_code',
            'js_code' => $jsCode,
        ];
        $query = http_build_query($params);
        $ret = curl_get("https://api.weixin.qq.com/sns/jscode2session?" . $query);
        if ($ret) {
            $data = json_decode($ret, true);
            if(isset($data['session_key'])) {
                unset($data['session_key']);
            }
            if ($data && isset($data['errcode']) && $data['errcode'] == 0) {
                $res['success'] = true;
                $res['desc'] = '';
                $res['data']['openid'] = isset($data['openid']) ? $data['openid'] : "";
                $res['errorcode'] = '';
                return json($res);
            }
            $res['success'] = true;
            $res['desc'] = '';
            $res['data'] = $data;
            $res['errorcode'] = '';
            return json($res);
        }
        $res['success'] = false;
        $res['desc'] = '微信接口错误';
        $res['data'] = $ret;
        $res['errorcode'] = '200022';
        return json($res);
    }

    public function getConfigInfo()
    {
        $res['success'] = true;
        $res['desc'] = '';
        $res['data']['help_desc'] = ['content' => '由于校园初建且人力有限，我们只有中午去集贤镇取快递时可以帮各位顺路代买所需要的用品，出于安全考虑目前代买范围仅限：蜜雪冰城、水果、糕点，单次收费3元（合理的个数跟重量。过多或过重另行协商）联系电话 <a class="help_tel">17392704241</a> 闫先生（微信同号）', 'tel_calss_label' => 'help_tel', 'tel' => 17392704241];
        $res['data']['dropshipping_desc'] = ['content' => '将自己要寄走的物品拿到校园超市内，联系店长进行代寄服务 联系电话 <a class="dropshipping_tel">17392704241 闫先生</a>（微信同号）', 'tel_calss_label' => 'dropshipping_tel', 'tel' => 17392704241];
        $res['data']['bottom_desc'] = ['content' => '每天下午 13 点前登记的快递当日下午可取 13 点后登记的次日下午取货 联系电话：17392704241（闫先生）', 'tel_calss_label' => '', 'tel' => 17392704241];
        $res['errorcode'] = '';
        return json($res);
    }
    public function getConfigDescInfo()
    {
        $res['success'] = true;
        $res['desc'] = '';
        $res['data']['tab'] = [
            [
                'form_list' => [
                    [
                        'label_name' => '选择快递',
                    ],
                    [
                        'label_name' => '收件人姓名',
                    ], [
                        'label_name' => '收件人电话',
                    ], [
                        'label_name' => '取件码',
                    ], [
                        'label_name' => '备注',
                    ]
                ],
                'form_title' => '批量下单',
                'tab_name' => '取快递',
            ],
            [
                'form_list' => [],
                'form_title' => '',
                'tab_name' => '代寄',
            ],
            [
                'form_list' => [],
                'form_title' => '',
                'tab_name' => '代买',
            ]
        ];
        $res['errorcode'] = '';
        return json($res);
    }
}
